package cn.edu.jnu.x2021100733;

public class BowlingGame {

    public int pins[] = new int[21];

    public int currentIndex = 0;

    public void roll(int pin) {
        pins[currentIndex++] = pin;
    }

    public int score() {
         int totalScore = 0;
         int currentFrameScoreIndex = 0;
         for(int currentFrame = 0;currentFrame < 10;++currentFrame){
             totalScore += pins[currentFrameScoreIndex];
             totalScore += pins[currentFrameScoreIndex + 1];
             if(isaStrike(currentFrameScoreIndex)){
                 totalScore += pins[currentFrameScoreIndex + 2];
             }
             else if(isaSpare(currentFrameScoreIndex)){
                 totalScore += pins[currentFrameScoreIndex + 2];
                 currentFrameScoreIndex++;
             }
             else{
                 currentFrameScoreIndex++;
             }
             currentFrameScoreIndex++;
         }

        return totalScore;
    }

    private boolean isaSpare(int scoreIndex) {
        return 10 == pins[scoreIndex] + pins[scoreIndex + 1];
    }
    private boolean isaStrike(int scoreIndex) {
        return 10 == pins[scoreIndex];
    }
}
